package com.shadow.demo.decompilation;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public final class FileUtils {

    private FileUtils() {
    }

    public static final String POM = "pom.xml";
    public static final String B_POM = ".pom";
    public static final String B_JAR = ".jar";
    public static final String PATH_S = "\\";

    public static boolean isPom(File file) {
        return file.getName().equals(POM);
    }

    public static boolean isPomFile(File file) {
        return file.isFile()
                && file.getName().endsWith(B_POM);
    }

    public static boolean isJarFile(File file) {
        return file.isFile()
                && file.getName().endsWith(B_JAR);
    }

    // 创建 a 目录
    public static File mkdir(File parentFile, String fileName) {
        File aDir = new File(parentFile, fileName);
        if (!aDir.exists() && !aDir.mkdirs()) {
            System.err.println("无法创建目录: " + aDir.getAbsolutePath());
            return null;
        }
        return aDir;
    }

    public static void copyAll(File source, File target) {
        if (source.isDirectory()) {
            // 在目标中创建对应目录
            if (!target.exists() && !target.mkdirs()) {
                System.out.println("copyAll, 目录创建失败");
                return;
            }
            File newTarget = new File(target, source.getName());
            if (!newTarget.exists() && !newTarget.mkdirs()) {
                System.out.println("copyAll, 目录创建失败2");
                return;
            }
            File[] files = source.listFiles();
            if (files != null) {
                for (File child : files) {
                    copyAll(child, newTarget); // 递归
                }
            }
        } else {
            copyFile(source, new File(target, source.getName()));
        }
    }

    public static void copyFile(File source, File target) {
        try (InputStream in = Files.newInputStream(source.toPath());
             OutputStream out = Files.newOutputStream(target.toPath())) {
            byte[] buffer = new byte[4096]; // 缓冲区
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            System.out.println("---文件复制成功：" + target.getPath());
        } catch (IOException e) {
            System.err.println("---文件复制失败：" + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void unzipFile(String resourcePath, String targetPath) {
        File targetDir = new File(targetPath);
        if (!targetDir.exists()) {
            System.out.println("---目标文件夹不存在: " + targetPath);
        }
        try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(resourcePath)))) {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                unZipEntry(zis, entry, targetPath);
                zis.closeEntry();
            }
        } catch (IOException e) {
            System.out.println("解压异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void unZipEntry(ZipInputStream zis, ZipEntry entry, String targetPath) throws IOException {
        File outFile = new File(targetPath, entry.getName());
        if (entry.isDirectory()) {
            if (outFile.exists()) {
                System.out.println("解压_目录_存在: " + outFile.getPath());
                return;
            }
            boolean isMake = outFile.mkdirs();
            if (!isMake) {
                System.out.println("解压_目录_创建失败1: " + outFile.getPath());
            }
        } else {
            File parentFile = outFile.getParentFile();
            if (!parentFile.exists()) {
                boolean isMake = parentFile.mkdirs();
                if (!isMake) {
                    System.out.println("解压_目录_创建失败2: " + outFile.getParentFile().getPath());
                }
            }
//            if (isPom(outFile)) {
//                File moduleDir = new File(targetPath, POM);
//                outPutFile(zis, moduleDir);
//            }
            outPutFile(zis, outFile);
        }
    }

    private static void outPutFile(ZipInputStream zis, File outFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(outFile)) {
            byte[] buffer = new byte[4096];
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        }
    }
}
